package com.example.demo.Impl;

import com.example.demo.dto.StudentRecordDTO;
import com.example.demo.entity.Organization;
import com.example.demo.entity.Student;
import com.example.demo.entity.TestRecord;
import com.example.demo.repository.StudentRepository;
import com.example.demo.repository.TestRecordRepository;
import com.example.demo.service.StudentManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class StudentManagementServiceImpl implements StudentManagementService {

    @Autowired
    private StudentRepository studentRepository;

    @Autowired
    private TestRecordRepository testRecordRepository;

    @Override
    public List<StudentRecordDTO> getAllStudentRecords() {
        List<Student> students = studentRepository.findAll();
        List<StudentRecordDTO> result = new ArrayList<>();

        for (Student student : students) {
            StudentRecordDTO dto = new StudentRecordDTO();
            dto.setStudentId(student.getId());
            dto.setStudentNo(student.getStudentNo()); // 这里是学号
            dto.setStudentName(student.getName());    // 这里是姓名

            // 组织结构遍历：班级 -> 专业 -> 学院
            Organization clazz = student.getClazz();  // 假设 Student 有 getClazz() 返回班级实体
            if (clazz != null) {
                dto.setClassName(clazz.getName());

                Organization major = clazz.getParent();
                if (major != null) {
                    dto.setMajorName(major.getName());

                    Organization college = major.getParent();
                    if (college != null) {
                        dto.setCollegeName(college.getName());
                    }
                }
            }

            // 取当前学生所有测试记录，按测试时间倒序
            List<TestRecord> records = testRecordRepository.findByStudent_IdOrderByTestTimeDesc(student.getId());
            List<StudentRecordDTO.Record> recordDTOs = new ArrayList<>();
            for (TestRecord record : records) {
                StudentRecordDTO.Record r = new StudentRecordDTO.Record();
                r.setRecordId(record.getId());
                r.setStudentNo(student.getStudentNo());   // 补充学生学号，方便前端展示
                r.setStudentName(student.getName());      // 补充学生姓名，方便前端展示
                r.setMbtiType(record.getResult());        // MBTI 类型字段
                r.setTestTime(record.getTestTime());      // 测试时间
                r.setAnalysis(mapAnalysis(record.getResult()));  // 结果分析
                recordDTOs.add(r);
            }
            dto.setRecords(recordDTOs);

            result.add(dto);
        }

        return result;
    }

    // 根据MBTI类型映射对应中文分析
    private String mapAnalysis(String mbtiType) {
        if (mbtiType == null) return "暂无分析";
        switch (mbtiType) {
            case "ISTJ": return "认真负责，实事求是的守护者。";
            case "ISFJ": return "忠诚体贴，乐于助人的保护者。";
            case "INFJ": return "理想主义者，注重内心感受和价值。";
            case "INTJ": return "战略家，擅长规划和解决复杂问题。";
            case "ISTP": return "冷静务实的实干家。";
            case "ISFP": return "温和谦逊，富有艺术气息。";
            case "INFP": return "理想主义者，富有同情心。";
            case "INTP": return "逻辑严密，善于分析。";
            case "ESTP": return "热情奔放，擅长应变。";
            case "ESFP": return "开朗活泼，善于社交。";
            case "ENFP": return "充满热情，富有创造力。";
            case "ENTP": return "机智灵活，喜欢挑战。";
            case "ESTJ": return "组织能力强，务实领导。";
            case "ESFJ": return "友善热心，注重人际关系。";
            case "ENFJ": return "有感染力的领导者。";
            case "ENTJ": return "果断坚定的执行者。";
            default: return "暂无分析";
        }
    }
}
